package com.it.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.product.domain.Category;
import com.it.product.domain.CategoryBrand;
import com.it.product.mapper.CategoryBrandMapper;
import com.it.product.service.ICategoryBrandService;
import com.it.product.service.ICategoryService;
import jakarta.servlet.ServletException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * projectName: spzx-parent
 *
 * @author: 王宝贵
 * time: 2024/9/20 20:03 周五
 * description:
 */
@Service
public class CategoryBrandServiceImpl  extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    @Autowired
    CategoryBrandMapper categoryBrandMapper;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 分页查询分类品牌
     * @param categoryBrand 查询非条件
     * @return
     */
    @Override
    public List<CategoryBrand> PageList(CategoryBrand categoryBrand) {

        return categoryBrandMapper.pageList(categoryBrand);
    }

    /**
     * 新增分类品牌
     * @param categoryBrand
     * @return
     */
    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) throws ServletException {
        //判断情况---改分类·已经·添加
        Long aLong = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>().
                eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()).
                eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
        );
        if(aLong>0){
            throw  new ServletException("该分类已经添加");
        }
        //添加到库里面
        return categoryBrandMapper.insert(categoryBrand);
    }


    /**
     * 修改分类品牌
     * @param categoryBrand
     * @return
     */
    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) throws ServletException {
        //判断情况1--未修改--根据主键·id可以查询到一个唯一的
        CategoryBrand categoryBrand1 = categoryBrandMapper.selectOne(new LambdaQueryWrapper<CategoryBrand>().
                eq(CategoryBrand::getId, categoryBrand.getId())
        );
        if(categoryBrand1.getBrandId() .equals(categoryBrand.getBrandId()) && categoryBrand1.getCategoryId().equals(categoryBrand.getCategoryId())){
            throw  new ServletException("请修改后再提交");
        }


        //判断情况2--数据库中存在数据
        Long aLong = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>().
                eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()).
                eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
        );
        if(aLong>0){
            throw  new ServletException("该分类已经添加");
        }
        return categoryBrandMapper.updateById(categoryBrand);
    }

    /**
     * 获取分类信息的详细信息
     * @param id cb的主键id
     * @return
     * 先传入CB的主键 根据主键查询到c的主键id
     * 然后根据主键id查询数据获取父类的id 递归 依次将主键id放到集合里面
     */
    @Override
    public CategoryBrand selectById(Long id) {
        CategoryBrand categoryBrand = categoryBrandMapper.selectById(id);
         List<Long> categoryIdList =  categoryService.selectListById(categoryBrand.getCategoryId());
         categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }
}